<?php

namespace web\api\controller;

use GuzzleHttp\Client;
use web\common\service\Credential;
use web\common\service\TronKit;
use web\common\service\TronApi;
use web\common\service\TronCoin;

/**
 * Description of Login
 * @author shilinqing
 */
class Test extends \web\common\controller\Controller {

    protected function _initialize() {

    }

    public function newaccount() {
        exit();
        $pass = 123;
        $credential = Credential::create();
        $address = (string)$credential->address();
        $trc['private_key'] = $credential->privateKey();
        $trc['public_key'] = $credential->publicKey();
        $trc['address'] = $address;
        // return $this->successJSON($trc);
        dump($trc);
        // return $trc;

    }

    /**
     * 测试
     */
    public function test(){
        $opts = [
            'base_uri' => 'http://47.99.130.24:8765',
        ];
        $client = new Client($opts);
        $payload = [
            'userid' =>"121",
            'timestamp'=>time()*1000,
            'integertype'=>'1',
            'mchid'=>'958760',
            'limit'=>"10",
            'page'=>'1',
            'asset'=>'PPB',
            'fodate'=>'2020-01-01 00:00:00',
            'todate'=>'2020-10-01 00:00:00',
        ];
        ksort($payload);
        $str = '';
        foreach ($payload as $key => $value) {
            $str.= $key.'='.$value.'&';
        }
        $str = $str.'secret_key=ceb2b48af80201e61437a4dc73bff341';
        dump($str);
        $payload['signature'] = strtoupper(md5($str));
        dump($payload);
        $opts = [
            'json' => $payload,
        ];
        $rsp = $client->post('/api/auth/jwt/mini/mall/consume/record', $opts);
        $content = $rsp->getBody();
        $res = json_decode($content,1);
        dump($res);
    }

    public function getamount(){
        $user_id = 47363;
        $address = (new \addons\member\model\Balance)->where(['user_id'=>$user_id,'type'=>1])->value('address');
        $res = (new \web\common\service\TronCoin)->amountUpdate($address,$user_id,1);
        // return $trc;

    }

    public function refund(){
        $last_id = 198;
        $type = 2;
        $url = 'http://app.m-pay.pro/api/login/outCodePayRe';
        $postdata['t'] = time();
        $apikey = '4cc6b1e5033f9c6f6284f30699265bd2';
        $postdata['sign'] = strtoupper(md5($postdata['t'].$apikey));
        $postdata['last_id'] = $last_id;
        $postdata['type'] = $type;
        $res = $this->posturl($url,$postdata);
        $list = json_decode($res,1);
        return $this->successJSON($list);
    }

    public function search(){
        $last_id = 0;
        $url = 'http://app.m-pay.pro/api/login/codePayRe';
        $postdata['t'] = time();
        $apikey = '4cc6b1e5033f9c6f6284f30699265bd2';
        $postdata['sign'] = strtoupper(md5($postdata['t'].$apikey));
        $postdata['last_id'] = $last_id;
        $res = $this->posturl($url,$postdata);
        $list = json_decode($res,1);
        return $this->successJSON($list);
    }

    private function posturl($url,$data){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }

   
}
